//
//  ByViewSubClassificationScroll.m
//  chequanbao_ios
//
//  Created by mc on 2017/10/10.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewSubClassificationScroll.h"

// vc
#import "ByUIShopsIndexVC.h"

@implementation ByCellSubClassificationItem
{
    UIImageView *_img;
    UILabel     *_cateLabel;
    UILabel     *_countLabel;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    _img = [[UIImageView alloc] init];
    [self.contentView addSubview:_img];
    
    _cateLabel = [[UILabel alloc] init];
    _cateLabel.font = FONT(16);
    [self.contentView addSubview:_cateLabel];
    
    _countLabel = [[UILabel alloc] init];
    _countLabel.font = FONT(14);
    _countLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_countLabel];
    
}
- (void)setItemModel:(ByModelShopCate *)itemModel{
    _itemModel = itemModel;
    
    CGFloat x,y,width,height;
    // 图片
    x = 5;
    y = 5;
    width = height = 40;
    _img.frame = CGRectMake(x, y, width, height);
    
    CGFloat class_width = [@"各地特色菜" getSizeOfString:14].width + 10 + 15 * 2 + 10 + [@"111" getSizeOfString:14].width;
    CGFloat sub_width = SCREEN_WIDTH - class_width - 20;
    // 数量
    width = [itemModel.count getSizeOfString:14].width + 10;
    height = 20;
    x = sub_width - width - 5;
    y = 15;
    _countLabel.frame = CGRectMake(x, y, width, height);
    
    // 名称
    x = _img.right + 10;
    y = 10;
    width = sub_width - x - width - 5;
    height = 30;
    _cateLabel.frame = CGRectMake(x, y, width, height);
    
    [_img setImageWithURL:[NSURL URLWithString:_itemModel.iconUrl] placeholderImage:DEFAULT_IMAGE];
    _cateLabel.text = _itemModel.name;
    _countLabel.text = _itemModel.count;
    
}

@end

#pragma mark - 主视图
@implementation ByViewSubClassificationScroll
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    if (self = [super initWithFrame:frame style:style]) {
        
        [self setupDefault];
    }
    return self;
}
// 创建表视图
- (void)setupDefault {
    
    self.backgroundColor = [UIColor whiteColor];
    self.delegate = self;
    self.dataSource = self;
    self.tableFooterView = [UIView new];
    
}



#pragma mark - UITableViewDataSource UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.itemModels.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellSubClassificationItem *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[ByCellSubClassificationItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    
    ByModelShopCate *model = self.itemModels[indexPath.section];
    cell.itemModel = model;
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ByUIShopsIndexVC *vc = [[ByUIShopsIndexVC alloc] init];
    NSLog(@"%@",self.itemModels);
    ByModelShopCate *model = self.itemModels[indexPath.section];
    vc.cate_id = model.cate_id;
    [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
}



@end
